using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class WebPartsDemo3 : System.Web.UI.Page
{
    protected void GenerateModeList(object sender, EventArgs e)
    {
        WebPartManager _manager = WebPartManager.GetCurrentWebPartManager(Page);

        String browseModeName = WebPartManager.BrowseDisplayMode.Name;

        drdModelist.Items.Clear();
        // Fill the drop-down list with the names of supported display modes.
        foreach (WebPartDisplayMode mode in _manager.SupportedDisplayModes)
        {
            String modeName = mode.Name;
            // Make sure a mode is enabled before adding it.
            if (mode.IsEnabled(_manager))
            {
                ListItem item = new ListItem(modeName, modeName);
                drdModelist.Items.Add(item);
            }
        }

        // Select the current mode
        ListItemCollection items = drdModelist.Items;
        int selectedIndex = items.IndexOf(items.FindByText(_manager.DisplayMode.Name));
        drdModelist.SelectedIndex = selectedIndex;
    }

    void Page_Init(object sender, EventArgs e)
    {
        Page.InitComplete += new EventHandler(GenerateModeList);
    }  

    protected void Page_Load(object sender, EventArgs e)
    {

    }

    protected void drdModelist_SelectedIndexChanged(object sender, EventArgs e)
    {
        WebPartManager _manager = WebPartManager.GetCurrentWebPartManager(Page);
        WebPartDisplayMode mode = _manager.SupportedDisplayModes[drdModelist.SelectedValue];
        if (mode != null)
            _manager.DisplayMode = mode;
    }
}
